import { Context, Schema } from 'koishi'

import layout from "./layout/index";

export const name = 'plantvszombie'

export interface Config {}

export const Config: Schema<Config> = Schema.object({})

export const inject = ['puppeteer', 'database'] // 注入插件

// ['puppeteer', 'database', 'assets', 'server']

import { UserInfo, Plant } from "./utils/type";

import { userSunDrop } from './database/userList'


declare module 'koishi' {
  interface Tables {
      userList: {
        id: string,
        name: string,
        userId: string,
        avatar: string,
        userInfo: UserInfo,
        plant: Array<Plant>,
      };

      dataUpdate: {
        id: string,
        time: string,
        info: string,
        data: object,
      }

      errorTotal: {
        id: string,
        time: string,
        error: string,
        messageData: object,
      }
  }
}


export function apply(ctx: Context) {

  // 数据库配置

  // 扩展表 user
  ctx.model.extend('userList', {
    id: 'text',
    name: 'text',
    userId: 'text',
    avatar: 'text',
    userInfo: 'json', // text类型长度为65535 较为安全
    plant: 'json',
  })

  // 扩展表 dataUpdate
  ctx.model.extend('dataUpdate', {
    id: 'text',
    time: 'text',
    info: 'text',
    data: 'json',
  })

  // 扩展表 errorTotal
  ctx.model.extend('errorTotal', {
    id: 'text',
    time: 'text',
    error: 'text',
    messageData: 'json',
  })

  // 定时任务
  // 每一分钟执行一次
  ctx.setInterval(() => {
    userSunDrop(ctx)
  }, 60 * 1000)


  layout(ctx)
}
